﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Medora.DataContract;
using Medora.Model.Institutions;
using Medora.Services.Utils;

namespace Medora.Services.Institutions
{
    public class ProvidersService : ServiceBase<Provider, ProviderFilter>, IProvidersService
    {
        private IProviderRepository _repository;

        public ProvidersService(IUow uow, IUserService userService, IFilterConstructor<ProviderFilter, Provider> filterConstructor)
            : base(uow, userService, filterConstructor)
        {
            _repository = _uow.Providers;
        }

        protected override Expression<Func<Provider, bool>> ConstructFilter(Expression<Func<Provider, bool>> startE, ProviderFilter filter)
        {
            if (filter == null) return startE;

            var finalE = startE;

            if (filter.DepartmentId.HasValue)
            {
                Expression<Func<Provider, bool>> departmentIdE = o => o.DepartmentId == filter.DepartmentId;
                finalE = _filterConstructor.And(finalE, departmentIdE);
            }
            
            if (!string.IsNullOrEmpty(filter.IDNP))
            {
                Expression<Func<Provider, bool>> idnpE =
                    o => o.IDNP.Contains(filter.IDNP);
                finalE = _filterConstructor.And(finalE, idnpE);
            }

            if (!string.IsNullOrEmpty(filter.FirstName))
            {
                Expression<Func<Provider, bool>> firstNameE =
                    o => o.FirstName.Contains(filter.FirstName);
                finalE = _filterConstructor.And(finalE, firstNameE);
            }

            if (!string.IsNullOrEmpty(filter.LastName))
            {
                Expression<Func<Provider, bool>> lastNameE =
                    o => o.LastName .Contains(filter.LastName);
                finalE = _filterConstructor.And(finalE, lastNameE);
            }
             
            if (!string.IsNullOrEmpty(filter.DepartmentName))
             {
                 Expression<Func<Provider, bool>> departmentNameE =
                     o => o.Department != null && o.Department.Title.Contains(filter.DepartmentName);
                 finalE = _filterConstructor.And(finalE, departmentNameE);
             }

             if (!string.IsNullOrEmpty(filter.SpecialityName))
             {
                 Expression<Func<Provider, bool>> specialityNameE =
                     o => o.Speciality != null && o.Speciality.Title.Contains(filter.SpecialityName);
                 finalE = _filterConstructor.And(finalE, specialityNameE);
             }

             if (!string.IsNullOrEmpty(filter.PositionName))
             {
                 Expression<Func<Provider, bool>> positionNameE =
                     o => o.Position != null && o.Position.Title.Contains(filter.PositionName);
                 finalE = _filterConstructor.And(finalE, positionNameE);
             }

            return finalE;


        }

        public IEnumerable<ProviderView> GetByDepartmentLazy(Guid id)
        {
            return _repository.GetLazy(o => o.DepartmentId == id).Select(x => new ProviderView { Id = x.Id, FirstName = x.FirstName, LastName = x.LastName, IDNP = x.IDNP, SpecialityTitle = x.Speciality.Title});
        }
    }

    public interface IProvidersService: IService<Provider,ProviderFilter>
    {
        IEnumerable<ProviderView> GetByDepartmentLazy(Guid id);
    }
    
    
}